package cn.gson.crm.controller.custom;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.CustomDao;
import cn.gson.crm.model.dao.DeptDao;
import cn.gson.crm.model.dao.EmpDao;
import cn.gson.crm.model.entity.Custom;
import cn.gson.crm.model.entity.Dept;
import cn.gson.crm.model.entity.Employee;
import cn.gson.crm.vo.CustomVo;
import cn.gson.crm.vo.EmployeeVo;

/**
 * customd的主界面
 */
@WebServlet("/custom")
public class CustomServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private CustomDao customDao = new CustomDao();
	private DeptDao deptDao = new DeptDao();
//	List<Dept> xiaoshou = new ArrayList<>();
	Dept xiaoshoubu=new Dept();

	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<Dept> deptList = deptDao.getSonDeptByUpdateId("1");
		String p = request.getParameter("page");
		String search = request.getParameter("search");
		if (deptList != null) {
			for (Dept dept : deptList) {
				if ("销售部".equals(dept.getDept_name())) {
//					xiaoshou.add(dept);
					xiaoshoubu=dept;
				}
			}
		}
		if (p == null || p.isEmpty()) {
			p = "1";
		}
		Integer page = Integer.parseInt(p);

		// 获得全部信息System.out.println(sql);
		List<CustomVo> customVoList = customDao.getEmpAllList(1, page, search);
		// 总页数
		int totalPage = customDao.getTotalPage(1, search);
		request.setAttribute("queryString", noPageQueryString(request));
		request.setAttribute("customList", customVoList);
		// request.setAttribute("empList", empList);
		request.setAttribute("totalPage", totalPage);
		request.setAttribute("page", page);

//		request.setAttribute("deptList", xiaoshou);
		request.setAttribute("xiaoshoubu", xiaoshoubu);
		forward("custom/custom.jsp");
	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	private String noPageQueryString(HttpServletRequest request) {
		Map<String, String[]> params = request.getParameterMap();

		String paramsStr = "";
		int i = 0;
		for (String key : params.keySet()) {
			if ("page".equals(key)) {
				continue;
			}
			String[] valus = params.get(key);
			String query = "";
			for (int j = 0; j < valus.length; j++) {
				query += key + "=" + valus[j];
				if (j < valus.length - 1) {
					query += "&";
				}
			}
			paramsStr += query;
			if (i < params.keySet().size() - 1) {
				paramsStr += "&";
			}
			i++;
		}
		return paramsStr.isEmpty() ? "" : "&" + paramsStr;
	}

}
